<#include "/WEB-INF/views/admin/common/layout/__mainlayout.html">
<#include "/WEB-INF/views/admin/common/__pagination.html">
<#assign navLink='admin:sys:settlement:list' />
<#assign pageCss>
<link rel="stylesheet" href="${ctx}/resource/validationengine/css/validationEngine.jquery.css">
</#assign>
<#assign pageScript>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">

    $(document).ready(function() {
        $("td[class^='textCut']").each(function() {
            var cutlength = 50;
            var text = $(this).html().trim();
            $(this).attr("title", text);
            text = (text.length > cutlength + 2) ? (text.substring(0, cutlength) + "...") : text;
            $(this).html(text);
        });

        laydate.render({
            elem: '#search_date'
            ,range: true
        });
        $('#search_agentname').chosen();

        $("#batch_pay").click(function(){
            var selectedItem = [];
            var statusItem = [];
            $(".checkbox:checked").each(function(){
                var status = $(this).parent().next().next().next().next().next().text().trim();
                statusItem.push(status);
                selectedItem.push($(this).val());
            });
            var statusAll = statusItem.join(',');
            if(statusAll.indexOf('approve')!=-1 || statusAll.indexOf('refuse')!=-1){
                layer.msg("只能导出new类型数据", {
                    icon : 2,
                    time : 1000,
                    end : function() {
                        window.location.reload();
                    }
                });
                return;
            }
            var selected = selectedItem.join(",");
            if(selected != ''){
                var myform = $("<form></form>");
                myform.attr('method','post')
                myform.attr('action',"${ctx}/admin/sys/settlement/export");
                var ids = $("<input type='hidden' name='id' />")
                ids.attr('value',selected);
                myform.append(ids);
                myform.appendTo('body').submit();
            }else{
                layer.msg("please choose data", {
                    icon : 2,
                    time : 1000,
                    end : function() {
                        window.location.reload();
                    }
                });
            }

        });
});

    function update(id) {
        layer.open({
            title: 'do settlement',
            type: 2,
            area: ['60%', '40%','no'],
            scrollbar: false,
            content: '${ctx}/admin/sys/settlement/update?id=' + id,
            end: function () {
                window.location.reload();
            }
        });

    }

    function selectAll(checked) {
        $('.checkbox').prop('checked',checked);
    }

    function changeSelectAll(){
        var items = $('.checkbox');
        var allSelect = $('#AllSelectChk');
        var flag = 1;
        items.each(function (e,ele) {
            if(!$(ele).prop('checked')){
                flag = 0;
            }
        })
        allSelect.prop('checked',!!flag);
    }

    function update(id,status) {
        var msg = "";
        var msg1 = "";
        if(status==1){
            msg = "Are you sure approve this apply?";
            msg1= "please enter the approve reason";
        }else{
            msg = "Are you sure refuse this apply?"
            msg1= "please enter the refuse reason";
        }
        layer.confirm(msg, {
            btn: ['yes','no'], //按钮

        }, function(index1){
            layer.close(index1);
            layer.prompt({title: msg1, formType: 2},function(text, index){
                $.ajax({
                    type : "POST",
                    url : "${ctx}/admin/sys/settlement/updateStatus",
                    data : {
                        "id" : id,
                        "status":status,
						"reason":text
                    },
                    success : function(userult) {
                        if (userult.isSuccess) {
                            layer.msg(userult.msg, {
                                icon : 1,
                                time : 1000,
                                end : function() {
                                    window.location.reload();
                                }
                            });
                        } else {
                            layer.msg(userult.msg, {
                                time : 1000,
                                icon : 2
                            });
                        }

                    }
                });
            });



            // 提交表单的代码，需要你自己写，然后用 layer.close 关闭就可以了，取消可以省略
            layer.close(index);
        });

    }
</script>
</#assign>
<@mainlayout>
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">settlement list</div>
                </div>
                <div class="widget-body  widget-body-lg am-fr">
                    <div class="am-u-sm-12 am-u-md-2 am-u-lg-3">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <@hasperm key='admin:sys:settlement:update'>
                                    <button id="batch_pay" type="button" class="am-btn am-btn-default am-btn-success">
                                        <span class="am-icon-paypal"></span> export
                                    </button>
                                </@hasperm>
                            </div>
                    </div>
                </div>
            </div>
            <form action="${ctx}/admin/sys/settlement/list/" method="post">
            		<div class="am-u-sm-12 am-u-md-2">
							<div
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								 <input
									type="text" class="am-form-field " name="search_id"
									value="${search_id}" placeholder="id">
							</div>
						</div>
            		<#if agents??>
            			<div class="am-u-sm-12 am-u-md-2">
								<div
									class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
									<select  name="search_agentname" id="search_agentname">
										<option value="0">please select agent</option>
										<#list agents as agent>
											<#if search_agentname?? && search_agentname == agent.id>
												<option value="${agent.id}" selected="selected">${agent.name}</option>
											<#else>
												<option value="${agent.id}">${agent.name}</option>
											</#if>
										</#list>
									</select>
								</div>
							</div>
					</#if>
				<div class="am-u-sm-12 am-u-md-2">
							<div
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								 <select name="search_status" id="search_status">
										<option value="-1">please select status</option>
										<#if search_status?? && search_status == 1>
											<option value="1" selected="selected">approve</option>
										<#else>
											<option value="1" >approve</option>
										</#if>
										<#if search_status?? && search_status == 0>
											<option value="0" selected="selected">new</option>
										<#else>
											<option value="0" >new</option>
										</#if>
										<#if search_status?? && search_status == 2>
											<option value="2" selected="selected">refuse</option>
										<#else>
											<option value="2" >refuse</option>
										</#if>
									</select>
							</div>
						</div>
                <div class="am-u-sm-12 am-u-md-3">
							<div
								class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
								<input type="text" class="am-form-field"  readonly name="search_date" value="${search_date}" id="search_date" placeholder="apply time">
								<span
									class="am-input-group-btn">
									<button
										class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
										type="submit"></button>
								</span>
							</div>
						</div>
            </form>
            <div class="am-scrollable-horizontal">
                <table class="am-table am-table-compact tpl-table-black am-text-truncate am-table-hover "
                       id="example-r">
                    <thead>
                    <tr>
                        <th class=""><input type="checkbox"
                                                                      onclick="selectAll(this.checked)" id="AllSelectChk"/></th>
                        <th class="">ID</th>
                        <th class="">agentName</th>
                        <th class="">day</th>
                        <th class="">amount</th>
                        <th class="">status</th>
                        <th class="">apply time</th>
                        <th class="">update time</th>
                        <th class="">reason</th>
                        <th class="">operation</th>
                    </tr>
                    </thead>
                    <tbody>
                    <#list pageList.list as settlement>
                    <tr class="gradeX">
                        <td class=""><input type="checkbox" onclick="changeSelectAll()" class="checkbox" name="id"
                                                                      value="${settlement.id!}"/></td>
                        <td class="">${settlement.id!}</td>
                        <td class="">${settlement.agentName!}</td>
                        <td class="textCut">${settlement.day!}</td>
                        <td class="">${settlement.amount!}</td>
                        <td class="">
                            <#if settlement.status == 1><font class="am-text-success">approve</font>
                            <#elseif settlement.status ==0><font class="am-text-danger">new</font>
                            <#elseif settlement.status ==2><font class="am-text-danger">refuse</font>
                        </#if>
                        </td>
                        <td class="">${settlement.createTime!}</td>
                        <td class="">${settlement.cleanTime!}</td>
                        <td class="">${settlement.reason!}</td>
                        <td class="">
                            <div class="tpl-table-black-operation">
                            <#if settlement.status == 0>
                                <@hasperm key='admin:sys:settlement:update'>
                                <a href="javascript:update('${settlement.id}',1);">
                                    <i class="am-icon-pencil"></i> approve
                                </a>
                            </@hasperm>
                            <@hasperm key='admin:sys:settlement:update'>
                                <a href="javascript:update('${settlement.id}',2);" class="tpl-table-black-operation-del">
                                    <i class="am-icon-pencil"></i> refuse
                                </a>
                            </@hasperm>
                           </#if> 
            </div>
            </td>
            </tr>
        </#list>
        <!-- more data -->
        </tbody>
        </table>
    </div>
    <@pagination page=pageList url='/admin/sys/settlement/list?' />
</div>
</div>
</div>
</div>
</div>
</@mainlayout>

